#include "blobb.h"
#include "GameApp.h"

//-------------------------------------------------------------------------------------
blobb::blobb(void): Actor("enemies/blobb.xml")
{
	mTypes.push_front(OBJECT_ENEMY);
	mTypes.push_front(OBJECT_BLOBB);
	mPause = 0.0f;
	mMinPause = 1.0f;
}
//-------------------------------------------------------------------------------------
blobb::~blobb(void)
{
}
//-------------------------------------------------------------------------------------
void blobb::update(const unsigned long elapsedTime)
{

	updateStun(elapsedTime);
	updateAirStatus(elapsedTime);

	if(mStates[MOVE_AIR])
	{
		if(getLocalVelocity().y() > 0.f)
			mMoveState = MOVE_JUMPUP;
		else mMoveState = MOVE_JUMPDOWN;

	} else mMoveState = MOVE_IDLE;

	mPause -= elapsedTime / 1000.0f;
	if (mPause < 0.0f) mPause = 0.0f;

	switch (mMoveState)
	{
	case MOVE_IDLE:
		if (mPause == 0.0f)
		{
			btVector3 targetPos = gpApp->mpGameLogic->getCharacter()->getBody()->getWorldTransform().getOrigin();
			btVector3 myPos = mpBody->getWorldTransform().getOrigin();

			btScalar range = myPos.distance(targetPos);

			if (range > moveRange) break;
			mpBody->activate();
		
			btMatrix3x3 &rot = mpBody->getWorldTransform().getBasis();
			btVector3 dir = targetPos - myPos;

			// Jump in player's direction
			btVector3 localDir = rot.inverse() * dir;
			if (localDir.x() > 0.0f)
			{
				setLocalVelocity(btVector3(5.0f, 15.0f, 0.0f));
				if (mStates[MOVE_LEFT])
				{
					lookRight();
				}
			}
			else
			{
				setLocalVelocity(btVector3(-5.0f, 15.0f, 0.0f));
				if (!mStates[MOVE_LEFT])
				{
					lookLeft();
				}
			}
			//mpAnimator->playAnimation(ANIM_FULL, Ogre::String("Jump_up"), false);
			//mMoveState = MOVE_JUMPUP;
			mPause = 1.0f + 2.f * rand()/(float)RAND_MAX;
			break;
		}
	case MOVE_JUMPUP:
		{
			mpAnimator->playAnimation(ANIM_FULL, Ogre::String("Blob_Jump_Up"));

			break;
		}
	case MOVE_JUMPDOWN:
			mpAnimator->playAnimation(ANIM_FULL, Ogre::String("Blob_Jump_Down"));
		break;
	default:
		return;
	}
}
//------------------------------------------------------------------------------------
void blobb::collision(BaseObjectPtr obj, btVector3 &point)
{
	switch (obj->getType())
	{
		case OBJECT_PLAYER:
			{
				ActorPtr actor = boost::static_pointer_cast<Actor>(obj);
				float damage = getStats()->getStat(STAT_DAMAGE);
				if(mStates[MOVE_AIR])
				{
					actor->recieveDmg(mpBody->getWorldTransform().getOrigin(), damage);
				}
				break;
			}
		default: return;
	}
}
//-------------------------------------------------------------------------------------
void blobb::scalePower(const char power)
{
	switch (power)
	{
	case 0:
		break;
	case 1:
		mMinPause = 0.5f;
		mHealthCap *= 1.5f;
		mHealth = mHealthCap;
		getStats()->setStat(STAT_DAMAGE, getStats()->getStat(STAT_DAMAGE) * 2.0f);
		break;
	case 2:
		mMinPause = 0.3f;
		mHealthCap *= 2.0f;
		mHealth = mHealthCap;
		getStats()->setStat(STAT_DAMAGE, getStats()->getStat(STAT_DAMAGE) * 3.0f);
		break;
	default:
		break;
	}
}